#!/usr/bin/perl -w
use strict;
#combine different bed files into one to expidite annotations
#takes as input the output from mysql into a text file in the format
#chromosome	start	finish	score
#MUST be in order of chromosome then start.  
#for now we will replace score with the total number
#of overlaps in that region
chomp(my $firstline = <STDIN>);
my ($prev_chromosome,$prev_start,$prev_finish) = split(/\t/,$firstline);
print "$prev_chromosome\t$prev_start\t";
my ($chromosome, $start, $finish);
my $score = 1;
while(my $line = <STDIN>) {
  chomp($line);
  ($chromosome, $start, $finish) = split(/\t/,$line);
  if($start > $prev_finish || $prev_chromosome ne $chromosome) { # finish the old one and start the new one.
    print "$prev_finish\t$score\n$chromosome\t$start\t";
    $score = 1;
    $prev_start = $start;
    $prev_finish = $finish;
    $prev_chromosome = $chromosome;
  } else { # combine this one with the previous keep the biggest finish
    $prev_chromosome = $chromosome;
    $prev_start  = $start;
    $score++;
    if($finish > $prev_finish) { $prev_finish = $finish; };
  }
}
print "$prev_finish\t$score\n";
